﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.COM;
using WCFMINERP.Factory.Presenter;
using WCFMINERP.Factory.Report;
using DevExpress.XtraReports.UI;


namespace WCFMINERP.Factory
{
    public partial class UCSercahProduct : XtraUserControl
    {
        public UCSercahProduct()
        {
            InitializeComponent();
        }
        Dictionary<int, string> dictList = new Dictionary<int, string>();
        OrderInfoPresenter _OrderinfoServer = new OrderInfoPresenter();
        Speech sopeech = new Speech();
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UCSercahProduct_Load(object sender, EventArgs e)
        {
            lbl_Mess.Text = "";
            if (Convert.ToBoolean(Help.ShowImg))
            {
                layout_showimg.Visibility= DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                layout_showimg.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }

            //PLX.StoreMode = 1;
            //PLX.FillThumbnail = false;
            //PLX.SelectionAntialiased = true;
            //PLX.EnableLoadEXIFThumbnails = false;
            //PLX.ThumbnailDisplayFilter = 7;
            //PLX.ThumbnailResampleFilter = 7;
            //PLX.EnableResamplingOnMinor = false;
            //PLX.EnableAdjustOrientation = true;
            //PLX.MultiSelecting = false;
            //PLX.ThreadPoolSize = 10;
            //PLX.ImageCacheSize = 300;
            //PLX.LookAhead = 500;
            //PLX.ThumbnailsInternalBorder = false;
            //PLX.EnableMultiSelect = true;
            //PLX.SelectionColor = Color.FromArgb(240, 108, 0);
            //PLX.SelectionStyle = 1;
            //PLX.MultiSelectionRegion = false;
            //PLX.SoftShadow = false;
            //PLX.ImageCacheUseDisk = false;
            //PLX.EnableImageCaching = true;
            //PLX.Width = 300;
            //PLX.Height = 300;
            //PLX.GridWidth = 1;
            //PLX.ThumbHeight = 150;
            //PLX.ThumbWidth = 150;
            //PLX.ThumbnailsBorderWidth = 0;
            //PLX.Size = new Size(300, 900);

            PLX.StoreMode = 1;
            PLX.FillThumbnail = false;
            PLX.SelectionAntialiased = false;
            PLX.EnableLoadEXIFThumbnails = false;
            PLX.ThumbnailDisplayFilter = 7;
            PLX.ThumbnailResampleFilter = 7;
            PLX.EnableResamplingOnMinor = false;
            PLX.EnableAdjustOrientation = true;
            PLX.MultiSelecting = false;
            PLX.ThreadPoolSize = 10;
            PLX.ImageCacheSize = 1024;
            PLX.LookAhead = 500;
            PLX.ThumbnailsInternalBorder = false;
            PLX.EnableMultiSelect = false;
            PLX.SelectionColor = Color.FromArgb(240, 108, 0);
            PLX.SelectionStyle = 1;
            PLX.MultiSelectionRegion = false;
            PLX.SoftShadow = false;
            PLX.ImageCacheUseDisk = false;
            PLX.EnableImageCaching = true;
            PLX.ThumbHeight = 150;
            PLX.ThumbWidth = 150;
            PLX.GridWidth = -1;
            PLX.Border = true;
            PLX.OnDblClick += PLX_OnDblClick;
        }
        
        void PLX_OnDblClick(object sender, EventArgs e)
        {
            frmimages image = new frmimages(dictList[PLX.SelectedImage]);
            image.ShowDialog();
        }

        OrderInfo _PrintOrderModel = null;
        /// <summary>
        /// 点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Code_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (int)Keys.Enter)
            {
                if (!string.IsNullOrEmpty(txt_Code.Text))
                {
                    int Stat = 2;
                    if ((10 - txt_Code.Text.Length) > 0)
                    {
                        
                        int d =(10 - txt_Code.Text.Length-1);
                        double dd=Math.Pow(10, d);
                        Stat = Convert.ToInt32(Stat * dd);
                        if (Stat == 1)
                        {
                            Stat = 2;
                        }
                        txt_Code.Text = Stat + txt_Code.Text;
                    }
                    
                    string mess = "";
                    string productMess = "";                    
                    OrderInfo OrderModel = _OrderinfoServer.SercahProduct(txt_Code.Text.TrimEnd(),out mess);
                    _PrintOrderModel = OrderModel;

                    if (OrderModel.Commit)
                    {
                        sopeech.Speak(mess);
                    }
                    else
                    {
                        sopeech.Speak(mess);
                    }
                    txt_Code.SelectAll();
                    lbl_Mess.Text = mess;
                    if (OrderModel.prodinfo != null)
                    {
                        lbl_PrdouctMess.Text = OrderModel.orderBagInfo.ShopName;
                        lbl_CtmName.Text = OrderModel.CtmName;
                        lbl_OrderProductCode.Text = OrderModel.OrderProductCode;
                        lbl_OrderProductName.Text = OrderModel.OrderProductName;
                        lbl_PrdouctName.Text = OrderModel.prodinfo.ProductName;

                        lbl_OrderRemark.Text = OrderModel.Remark;
                        lbl_MnemonicCode.Text = OrderModel.prodinfo.MnemonicCode;
                        lbl_Size.Text = OrderModel.prodinfo.Width + "X" + OrderModel.prodinfo.Height;
                        lbl_ProductDes.Text = OrderModel.prodinfo.ProductDes;
                        lbl_strStatus.Text = OrderModel.strStatus;
                        lbl_PageNum.Text = OrderModel.photoCount.ToString();
                        lbl_OrderTime.Text = OrderModel.orderBagInfo.OrderTime.ToString();
                        lbl_FacthTime.Text = OrderModel.FacthTime.ToString();
                        lbl_Remark.Text = OrderModel.orderBagInfo.Remark;
                        WriteAnalyseLog.WriteLogFile(DateTime.Now.ToString() + OrderModel.OrderPath);
                        if (OrderModel.OrderPath != null)
                        {
                            if (Convert.ToBoolean(Help.ShowImg))
                            {
                                PLX.Clear();
                                dictList.Clear();
                                BindImg(OrderModel.OrderPath);
                            }
                        }
                    }
                    else
                    {
                        PLX.Clear();
                        dictList.Clear();
                        lbl_PrdouctMess.Text = "";
                    }
                }
            }
        }

        /// <summary>
        /// 绑定图片
        /// </summary>
        /// <param name="orderpath"></param>
        public void BindImg(string orderpath)
        {
            if (System.IO.Directory.Exists(@"" + orderpath))
            {                
                string[] files = System.IO.Directory.GetFiles(@"" + orderpath);
                foreach (string file in files)
                {
                    if (file.EndsWith(".jpg") || file.EndsWith(".png") || file.EndsWith(".JPG") || file.EndsWith(".PNG"))
                    {
                        int i = PLX.AppendImage();

                        PLX.SetImageFileName(i, file);
                        dictList.Add(i, file);
                        PLX.Paint();
                    }
                }
            }
        }

        /// <summary>
        /// 打印条码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_printCode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Help.PrintName))
            {
                XtraMessageBox.Show("请先设置打印机!");
                return;
            }

            if (_PrintOrderModel == null || string.IsNullOrEmpty(_PrintOrderModel.OrderProductCode))
            {
                XtraMessageBox.Show("未找到条码!");
                return;
            }
            if (Convert.ToBoolean(Help.isPring))   //只有true才打印
            {
                try
                {
                    XtraReportProduct2 xrmLog = new XtraReportProduct2(_PrintOrderModel);
                    xrmLog.Print(Help.PrintName);
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                }
            }
            else
            {
                XtraMessageBox.Show("请设置为允许打印!");
                return;
            }
        }
    }
}
